﻿


Public Class Form1
    'CAMPO RICERCA
    Dim stringa As String
    'LISTA FILE DA BACKGROUNDWORKER
    Dim files As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
    Dim file As String
    Dim RICERCA_LIST As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
    Dim cartella As String
    'VIA LIBERA PER IL BACKGROUNDWORKER COMMESSE
    Dim statocommesse As Integer = 0
    'VIA LIBERA PER IL BACKGROUNDWORKER FILE
    Dim statofiles As Integer = 0
    Dim RIGA_COMP_GESTITI = 0
    Dim QUANTITA_COMPONENTE As String
    Dim PERCORSO_RICERCA As String = ""
    Dim RICERCA_COMPLETATA As Integer = 0
    Dim temp1 As System.IO.FileInfo
    Dim DATASET As New System.Data.DataSet




    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'PULISCO LA LISTBOX1

        CONTROL_COM_NON_GESTITI.OTTIENI_QUANTITA()
        ListBox1.Items.Clear()
        ListBox2.Items.Clear()
        'IMPOSTO LA STRINGA DI RICERCA
        stringa = TextBox1.Text

        If stringa.Contains("/") Then
            stringa = stringa.Replace("/", "_")
        End If
        If stringa.Contains("\") Then
            stringa = stringa.Replace("\", "_")
        End If

        'PUNTO INIZIO PER CONTEGGIO TEMPO
        Dim start = Now.TimeOfDay


        'SE IL CHECKBOX COMMESSE è BIANCO CERCO IN DEFINITIVI MODIFICA E PROVVISORI
        If CheckBox2.Checked = False Then

            CONTROL_COM_NON_GESTITI.DATASET(DATASET)


            If BackgroundWorker1.IsBusy = False Then
                RICERCA_COMPLETATA = 0
                PERCORSO_RICERCA = "\\Lafer2k3\uff_tec\PDF_PROE\"
                BackgroundWorker1.RunWorkerAsync()
                ToolStripStatusLabel2.Text = "Sto cercando in " & PERCORSO_RICERCA
            Else
            End If

            While RICERCA_COMPLETATA = 0
                Application.DoEvents()
                Continue While
            End While

            For Each stringa1 In RICERCA_LIST
                ProgressBar1.Maximum = RICERCA_LIST.Count
                If ProgressBar1.Value < ProgressBar1.Maximum Then
                    ProgressBar1.Value += 1
                Else
                End If

                If Chballfile.Checked = True Then
                    ListBox1.Items.Add(stringa1)
                ElseIf stringa1.EndsWith(".PDF") Or stringa1.EndsWith(".pdf") Then
                    ListBox1.Items.Add(stringa1)
                    Dim INSERITO = 0
                    For Each ITEM As System.Data.DataRow In DATASET.Tables(0).Rows
                        documento = New ClsPdfDocument(stringa1)
                        If ITEM.Item("CODICE").ToString = documento.PDFDOCUMENT.NAME Then
                            ListBox2.Items.Add(ITEM.Item("QUANTITA").ToString)
                            documento.PDFDOCUMENT.QUANTITA = ITEM.Item("QUANTITA").ToString
                            INSERITO = 1
                        End If
                    Next
                    If INSERITO = 0 Then
                        ListBox2.Items.Add(0)
                    End If

                End If
            Next

            If BackgroundWorker1.IsBusy = False Then
                RICERCA_COMPLETATA = 0
                PERCORSO_RICERCA = "\\Lafer2k3\uff_tec\Z_PDF_modifica_PROE"
                BackgroundWorker1.RunWorkerAsync()
                ToolStripStatusLabel2.Text = "Sto cercando in " & PERCORSO_RICERCA
            Else
            End If

            While RICERCA_COMPLETATA = 0
                Application.DoEvents()
                Continue While
            End While

            For Each stringa1 In RICERCA_LIST
                ProgressBar1.Maximum = RICERCA_LIST.Count
                If ProgressBar1.Value < ProgressBar1.Maximum Then
                    ProgressBar1.Value += 1
                Else
                End If
                If Chballfile.Checked = True Then
                    ListBox1.Items.Add(stringa1)

                ElseIf stringa1.EndsWith(".PDF") Or stringa1.EndsWith(".pdf") Then
                    ListBox1.Items.Add(stringa1)
                    Dim INSERITO = 0
                    For Each ITEM As System.Data.DataRow In DATASET.Tables(0).Rows
                        documento = New ClsPdfDocument(stringa1)
                        If ITEM.Item("CODICE").ToString = documento.PDFDOCUMENT.NAME Then
                            ListBox2.Items.Add(ITEM.Item("QUANTITA").ToString)
                            documento.PDFDOCUMENT.QUANTITA = ITEM.Item("QUANTITA").ToString
                            INSERITO = 1
                        End If
                    Next
                    If INSERITO = 0 Then
                        ListBox2.Items.Add(0)
                    End If
                End If

            Next

            If BackgroundWorker1.IsBusy = False Then
                RICERCA_COMPLETATA = 0
                PERCORSO_RICERCA = "\\Lafer2k3\uff_tec\Z_PDF_Prov_PROE"
                BackgroundWorker1.RunWorkerAsync()
                ToolStripStatusLabel2.Text = "Sto cercando in " & PERCORSO_RICERCA
            Else
            End If

            While RICERCA_COMPLETATA = 0
                Application.DoEvents()
                Continue While
            End While

            For Each stringa1 In RICERCA_LIST
                ProgressBar1.Maximum = RICERCA_LIST.Count
                If ProgressBar1.Value < ProgressBar1.Maximum Then
                    ProgressBar1.Value += 1
                Else
                End If
                If Chballfile.Checked = True Then
                    ListBox1.Items.Add(stringa1)
                ElseIf stringa1.EndsWith(".PDF") Or stringa1.EndsWith(".pdf") Then
                    ListBox1.Items.Add(stringa1)
                    Dim INSERITO = 0
                    For Each ITEM As System.Data.DataRow In DATASET.Tables(0).Rows
                        documento = New ClsPdfDocument(stringa1)
                        If ITEM.Item("CODICE").ToString = documento.PDFDOCUMENT.NAME Then
                            ListBox2.Items.Add(ITEM.Item("QUANTITA").ToString)
                            documento.PDFDOCUMENT.QUANTITA = ITEM.Item("QUANTITA").ToString
                            INSERITO = 1
                        End If
                    Next
                    If INSERITO = 0 Then
                        ListBox2.Items.Add(0)
                    End If
                End If
            Next

            If BackgroundWorker1.IsBusy = False Then
                RICERCA_COMPLETATA = 0
                PERCORSO_RICERCA = "\\Lafer2k3\uff_tec\Scansioni"
                BackgroundWorker1.RunWorkerAsync()
                ToolStripStatusLabel2.Text = "Sto cercando in " & PERCORSO_RICERCA
            Else
            End If

            While RICERCA_COMPLETATA = 0
                Application.DoEvents()
                Continue While
            End While

            '--------------------cerco in scansioni-----------------
            For Each stringa1 In RICERCA_LIST
                ProgressBar1.Maximum = RICERCA_LIST.Count
                If ProgressBar1.Value < ProgressBar1.Maximum Then
                    ProgressBar1.Value += 1
                Else
                End If
                If Chballfile.Checked = True Then
                    ListBox1.Items.Add(stringa1)
                ElseIf stringa1.EndsWith(".PDF") Or stringa1.EndsWith(".pdf") Then
                    ListBox1.Items.Add(stringa1)
                End If
            Next
            '-------------------------------------------------------
            ToolStripStatusLabel2.Text = "Ricerca completata"
        End If

        'SE DXF SERIE è SPUNTATO CERCO IN DXF
        If CheckBox1.Checked = True Then
            If BackgroundWorker1.IsBusy = False Then
                RICERCA_COMPLETATA = 0
                PERCORSO_RICERCA = "\\Lafer2k3\uff_tec\DXF\"
                BackgroundWorker1.RunWorkerAsync()
                ToolStripStatusLabel2.Text = "Sto cercando in " & PERCORSO_RICERCA
            Else
            End If

            While RICERCA_COMPLETATA = 0
                Application.DoEvents()
                Continue While
            End While

            For Each stringa1 In RICERCA_LIST
                ProgressBar1.Maximum = RICERCA_LIST.Count
                If ProgressBar1.Value < ProgressBar1.Maximum Then
                    ProgressBar1.Value += 1
                Else
                End If
                If Chballfile.Checked = True Then
                    ListBox1.Items.Add(stringa1)
                ElseIf stringa1.EndsWith(".DXF") Or stringa1.EndsWith(".dxf") Then
                    ListBox1.Items.Add(stringa1)
                End If
            Next
            ToolStripStatusLabel2.Text = "Ricerca completata"
        End If

        'SE COMMESSE è SPUNTATO CERCO IN COMMESSE
        If CheckBox2.Checked = True Then

            If rcommesse.Count > 0 Then
                For Each Me.cartella In rcommesse
                    ProgressBar1.Maximum = rcommesse.Count
                    ProgressBar1.Value += 1
                    Label3.Text = cartella.ToString
                    If bkwfiles.IsBusy = False Then
                        bkwfiles.RunWorkerAsync()
                    Else

                    End If

                    While statofiles = 0
                        Application.DoEvents()
                        Continue While
                    End While

                    For Each Me.file In files
                        If Chballfile.Checked = True Then
                            ListBox1.Items.Add(file)
                        ElseIf file.EndsWith(".DXF") Or file.EndsWith(".dxf") Or file.EndsWith(".PDF") Or file.EndsWith(".pdf") Then
                            ListBox1.Items.Add(file)
                        End If
                    Next
                    Application.DoEvents()
                    statofiles = 0
                Next
                'For Each stringa1 In My.Computer.FileSystem.GetFiles("\\Lafer2k3\uff_tec\Commesse\", FileIO.SearchOption.SearchAllSubDirectories, stringa & "*.*")
                'If stringa1.EndsWith(".DXF") Or stringa1.EndsWith(".dxf") Or stringa1.EndsWith(".PDF") Or stringa1.EndsWith(".pdf") Then
                'ListBox1.Items.Add(stringa1)
                'End If
                '   Next
            Else
                For Each Me.cartella In commesse
                    ProgressBar1.Maximum = commesse.Count
                    ProgressBar1.Value += 1
                    Label3.Text = cartella.ToString
                    If bkwfiles.IsBusy = False Then
                        bkwfiles.RunWorkerAsync()
                    Else

                    End If

                    While statofiles = 0
                        Application.DoEvents()
                        Continue While
                    End While

                    For Each Me.file In files
                        If Chballfile.Checked = True Then
                            ListBox1.Items.Add(file)
                        ElseIf file.EndsWith(".DXF") Or file.EndsWith(".dxf") Or file.EndsWith(".PDF") Or file.EndsWith(".pdf") Then
                            ListBox1.Items.Add(file)
                        End If

                    Next
                    Application.DoEvents()
                    statofiles = 0
                Next
                'For Each stringa1 In My.Computer.FileSystem.GetFiles("\\Lafer2k3\uff_tec\Commesse\", FileIO.SearchOption.SearchAllSubDirectories, stringa & "*.*")
                'If stringa1.EndsWith(".DXF") Or stringa1.EndsWith(".dxf") Or stringa1.EndsWith(".PDF") Or stringa1.EndsWith(".pdf") Then
                'ListBox1.Items.Add(stringa1)
                'End If
                '   Next
            End If

        End If

        Label3.Text = ""
        ProgressBar1.Value = 0

        'FERMO IL CONTATORE E VISUALIZZO IL TEMPO IMPIEGATO PER LA RICERCA
        Dim fine = Now.TimeOfDay
        Dim tempo = (fine - start)
        Label2.Text = "Tempo impiegato per la ricerca= " & tempo.Minutes.ToString & " min " & tempo.Seconds.ToString & " sec " & tempo.Milliseconds.ToString & " mil "

        'se trovo solo un file
        If ListBox1.Items.Count = 1 Then
            'e il file è un pdf
            If ListBox1.Items.Item(0).ToString.EndsWith(".pdf") Then
                'seleziono la prima riga
                ListBox1.SelectedItem = ListBox1.Items.Item(0)
                'se invece il file è un dxf
            ElseIf ListBox1.Items.Item(0).ToString.EndsWith(".dxf") Then
                'seleziono la prima riga
                ListBox1.SelectedItem = ListBox1.Items.Item(0)
            End If
        End If

        ListBox1.Focus()

        'SE NON TROVO NIENTE VISUALIZZO UN MESSAGGIO
        If ListBox1.Items.Count = 0 Then
            Label4.Text = "Nessun disegno trovato"

            TextBox1.SelectAll()
            TextBox1.Focus()
            'MsgBox("Nessun disegno trovato")
        End If

    End Sub

    Private Sub ListBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ListBox1.KeyDown
        If e.KeyCode = Keys.Delete Then
            System.IO.File.Delete(ListBox1.SelectedItem.ToString)
            Label4.Text = "File eliminato"
            'MsgBox("File eliminato")
            Button1_Click(sender, e)
        End If
    End Sub

    Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
        'SE CLICCO IL TASTO DX DEL MOUSE SU QUALCOSA NELLA LISTBOX VISUALIZZO IL MENU A TENDINA
        If ListBox1.SelectedItem <> Nothing Then
            If MouseButtons = Windows.Forms.MouseButtons.Right Then
                ListBox1.ContextMenuStrip = ContextMenuStrip1
                ListBox1.ContextMenuStrip.Show()
            End If
        End If
    End Sub


    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        'SE SELEZIONO QUALCOSA NELLA LISTBOX
        Label4.Text = ""
        documento = New ClsPdfDocument(ListBox1.SelectedItem.ToString, True)

        Try
            'VISUALIZZO LA TOOLTIP
            ToolTip1.AutoPopDelay() = 1200
            ToolTip1.SetToolTip(ListBox1, documento.PDFDOCUMENT.PERCORSO_COMPLETO)
            'SE IL FILE SELEZIONATO NON è UN PDF
            If documento.PDFDOCUMENT.ESTENSIONE.ToUpper = ".pdf".ToUpper = False Then
                ' MsgBox("Attenzione il file selezionato non è un PDF")
                'ABILITO IL PULSANTE ELIMINA
                Button3.Enabled = True
                'SE SELEZIONO UN FILE DXF
                If documento.PDFDOCUMENT.ESTENSIONE.ToUpper = ".dxf".ToUpper Then
                    Selezionefiledxf()
                End If
            Else
                'ALTRIMENTI SE IL FILE è UN PDF
                ToolStripStatusLabel1.Text = documento.PDFDOCUMENT.NAME
                ToolStripStatusLabel4.Text = documento.PDFDOCUMENT.MODIFICA
                Selezionefilepdf()
                Try
                    CONTROL_COM_NON_GESTITI.DATASET(DATASET)
                    ListBox2.SelectedIndex = ListBox1.SelectedIndex
                    'ListBox1.SelectedIndex = ListBox2.SelectedIndex
                    QUANTITA_COMPONENTE = ListBox2.SelectedItem.ToString
                    CONTROL_COM_NON_GESTITI.codice_comp = documento.PDFDOCUMENT.NAME
                    ListBox2.TopIndex = ListBox1.TopIndex
                Catch EX As Exception
                    QUANTITA_COMPONENTE = 0
                End Try
                Openpdffile()
                ListBox1.Focus()

            End If

        Catch EX As NullReferenceException

        End Try

        ListBox1.Focus()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        AxAcroPDF1.printAllFit(True)
        Label4.Text = "Documento inviato alla stampante"
        'MsgBox("Documento inviato alla stampante")
        'While bkwprint.IsBusy
        'Application.DoEvents()
        'End While
        'bkwprint.RunWorkerAsync()

    End Sub

    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        'SE PREMO ENTER ALL'INTERNO DELLA TEXTBOX RICHIAMO L'EVENTO DEL PULSANTE CERCA
        If e.KeyCode = Keys.Enter Then
            Button1_Click(sender, e)
        End If
    End Sub

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

        If e.KeyCode = Keys.Cancel Then
            Button3_Click(sender, e)
        End If

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Application.EnableVisualStyles()

        If My.Computer.FileSystem.FileExists(Application.StartupPath & "\primoavvio") = True Then
        Else
            Frmprimoavvio.Show()
        End If

        'CONTROLLO SE ESISTE LA CHIAVE PER ELIMINARE IL MESSAGGIO DI CONFERMA DI STAMPA DIRETTA
        If My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\Adobe\Acrobat Reader\10.0\AVAlert\cCheckbox\cEWH", "iWarnScriptPrintAll", 1) = Nothing Then
            'Process.Start(Application.StartupPath & "\CHIAVE_STAMPA_DIRETTA_ACROBAT_X.REG")
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\Adobe\Acrobat Reader\10.0\AVAlert\cCheckbox\cEWH", "iWarnScriptPrintAll", 1)
        End If

        'DISABILITO IL CHECKBOX COMMESSE
        CheckBox2.Enabled = False
        'VISUALIZZO IL MESSAGGIO
        Label3.Text = "Sto caricando la cartella commesse"
        AxVecad1.Registration(0)
        Me.Text = ("DrawSearch Suite for Lafer ver." & Application.ProductVersion)
        'NASCONDO VECAD
        AxVecad1.Hide()
        Label4.Text = ""
        ToolStripStatusLabel1.Text = ""
        ToolStripStatusLabel4.Text = ""
        Label2.Text = ""
        'DISABILITO STAMPA
        Button2.Enabled = False
        'DISABILITO ELIMINA
        Button3.Enabled = False
        'DISABILITO RINOMINA
        Button4.Enabled = False
        'DISABILITO STAMPA SPECIALE
        'Button7.Enabled = False
        BtnPrintX2.Enabled = False
        BtnPrintX3.Enabled = False
        'DISABILITO L'INGRESSO NEL FORM DISEGNI CLIENTI
        VisualizzaDisegniInCartelleClientiToolStripMenuItem.Enabled = False
        'LEGGO IL FILE CON LE CARTELLE COMMESSE SALVATE
        leggi_impostazioni()
        'RICREO LA LISTA DELLE CARTELLE COMMESSE
        bkwcommesse.RunWorkerAsync()
        'SE LA LISTA DELLE CARTELLE SALVATE ESISTE
        If rcommesse.Count > 0 Then
            'ABILITO LA PARTE DI COMMESSE
            CheckBox2.Enabled = True
            Label3.Text = "Ora è possibile eseguire ricerche in commesse"
            VisualizzaDisegniInCartelleClientiToolStripMenuItem.Enabled = True
        End If
        ToolStripStatusLabel2.Text = ""
        Dati_stampa1.Enabled = False

        'funzione per aprire i pdf con doppio click sul file
        Dim file As String = ""
        If My.Application.CommandLineArgs.Count > 0 Then
            file = My.Application.CommandLineArgs.First()
            ListBox1.Items.Add(file)
            ListBox1.SelectedItem = ListBox1.Items.Item(0)
        End If

        If My.Computer.FileSystem.DirectoryExists(Application.StartupPath & "\Temp") Then
        Else
            My.Computer.FileSystem.CreateDirectory(Application.StartupPath & "\Temp")
        End If

        FrmUpdate.Update_control1.version_application = Application.ProductVersion
        UPDATER_CONTROL.update_control.CONTROLLO_AGGIORNAMENTI()
        If My.Computer.FileSystem.FileExists(Application.StartupPath & "\Temp\filedaaggiornare") Then
            Dim messaggio = MsgBox("é presente una versione aggiornata di drawsearch, vuoi aggiornare?", MsgBoxStyle.YesNo)

            If messaggio = MsgBoxResult.Yes Then
                Process.Start(Application.StartupPath & "\copia_file.exe")
                Application.DoEvents()
            ElseIf messaggio = MsgBoxResult.No Then
                My.Computer.FileSystem.DeleteFile(Application.StartupPath & "\Temp\filedaaggiornare")
            End If


        End If

        Application.DoEvents()
    End Sub


    Private Sub TextBox1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.MouseHover
        'ToolTip1.Hide(TextBox1)
        ' ToolTip1.Show("Immetti il testo da cercare", TextBox1)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        If MsgBox("Vuoi veramente cancellare il file selezionato?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            'inizio istruzioni selezione singola
            'System.IO.File.Delete(ListBox1.SelectedItem.ToString)
            'fine istruzioni selezione singola
            'PER OGNI FILE SELEZIONATO ELIMINO IL FILE
            For Each Me.file In ListBox1.SelectedItems
                System.IO.File.Delete(file.ToString)
            Next
            Label4.Text = "File eliminato"
            ' MsgBox("File eliminato")
            'RIPETO LA RICERCA
            Button1_Click(sender, e)
        End If

    End Sub

    Private Sub VisualizzaNellaCartellaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VisualizzaNellaCartellaToolStripMenuItem.Click
        'APRO EXPLORER E PUNTO SUL FILE SELEZIONATO
        Shell("EXPLORER /SELECT," & ListBox1.SelectedItem.ToString, AppWinStyle.NormalFocus)
    End Sub

    Private Sub PDFPROEToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PDFPROEToolStripMenuItem.Click
        Try
            ' blocco istruzioni selezione singola
            ' Dim filesorgente = ListBox1.SelectedItem.ToString
            ' Dim filename = My.Computer.FileSystem.GetName(ListBox1.SelectedItem.ToString)
            'Dim directory = My.Computer.FileSystem.GetParentPath()
            '  Dim directory1 = filename.Remove(1, filename.Length - 1)
            '  My.Computer.FileSystem.CopyFile(filesorgente, "\\Lafer2k3\uff_tec\PDF_PROE\" & directory1 & "\" & filename)
            '   MsgBox("Disegno spostato in DEFINITIVI")
            '  My.Computer.FileSystem.DeleteFile(filesorgente)
            'fine istruzioni selezione singola

            Dim filesorgente = ListBox1.SelectedItems
            Dim filename As String
            Dim estensione As String
            For Each Me.file In filesorgente
                filename = My.Computer.FileSystem.GetName(file)
                estensione = filename.Remove(0, filename.Count - 4)
                filename = filename.Remove(filename.Count - 4, 4)
                Dim directory1 = filename.Remove(1, filename.Length - 1)
                comp_pdf.dati_stampa.INS_MODIFICA("", file)
                My.Computer.FileSystem.CopyFile(file, "\\Lafer2k3\uff_tec\PDF_PROE\" & directory1 & "\" & filename & estensione)
                My.Computer.FileSystem.DeleteFile(file)
            Next
            Label4.Text = "Disegno spostato in DEFINITIVI"
            'MsgBox("Disegno spostato in DEFINITIVI")
            Button1_Click(sender, e)

        Catch ex As Exception

        End Try

    End Sub

    Private Sub ZPDFMODIFICAToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ZPDFMODIFICAToolStripMenuItem.Click
        Try
            ' blocco istruzioni selezione singola
            ' Dim filesorgente = ListBox1.SelectedItem.ToString
            '  Dim filename = My.Computer.FileSystem.GetName(ListBox1.SelectedItem.ToString)
            'Dim directory = My.Computer.FileSystem.GetParentPath()
            ' Dim directory1 = filename.Remove(1, filename.Length - 1)
            '  My.Computer.FileSystem.CopyFile(filesorgente, "\\Lafer2k3\uff_tec\Z_PDF_modifica_PROE\" & directory1 & "\" & filename)
            '  MsgBox("Disegno spostato in MODIFICA")
            '  My.Computer.FileSystem.DeleteFile(filesorgente)
            'fine istruzioni selezione singola


            Dim filesorgente = ListBox1.SelectedItems
            Frmsposta_in_modifica.crea_sorgente(filesorgente)
            Frmsposta_in_modifica.Show()




            'Label4.Text = "Disegno spostato in MODIFICA"
            ' MsgBox("Disegno spostato in MODIFICA")
            ' Button1_Click(sender, e)

        Catch ex As Exception

        End Try

    End Sub

    Private Sub InformazioniToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InformazioniToolStripMenuItem.Click
        Informazioni.Show()

    End Sub

    Private Sub ContattaSviluppatoreToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContattaSviluppatoreToolStripMenuItem.Click
        Process.Start("mailto:andrea@lafer.it?SUBJECT=Segnalazione Migliorie/Anomalie")
    End Sub

    Private Sub VediChangelogToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VediChangelogToolStripMenuItem.Click

        Process.Start(Application.StartupPath & "\Changelog.txt")

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

        If ListBox1.SelectedItem <> Nothing Then
            Frmrename.ShowDialog(Me)
        End If

    End Sub

    Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
        If CheckBox2.CheckState = CheckState.Checked Then
            'If MsgBox("Attenzione cercare nella cartella commesse può richiedere un pò di tempo in media dai 30 secondi ai 2 minuti. Continuare?", MsgBoxStyle.YesNo) = MsgBoxResult.No Then
            'CheckBox2.Checked = False
            'End If
        End If
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

        If ListBox1.SelectedItem.ToString.EndsWith(".pdf") Then
            Dim processo = Process.Start(My.Computer.FileSystem.SpecialDirectories.ProgramFiles & "\pdf editor 2\pdfedit.exe", ListBox1.SelectedItem.ToString)
            While Not processo.HasExited
                Continue While
            End While
        End If

        AxAcroPDF1.Update()

    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click


        OpenFileDialog1.Filter() = "File PDF (*.pdf)|*.pdf|File DXF (*.dxf)|*.dxf|Tutti i file (*.*)|*.*"
        OpenFileDialog1.Multiselect = True
        OpenFileDialog1.ShowDialog()


        Dim files = OpenFileDialog1.FileNames
        ListBox1.Items.Clear()

        For Each item In files
            If item.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase) Then
                AxVecad1.Visible = False
                ListBox1.Items.Add(item)
                'AxAcroPDF1.LoadFile(item)
                Button2.Enabled = True
            End If

            If item.EndsWith(".dxf", StringComparison.OrdinalIgnoreCase) Then
                AxAcroPDF1.Visible = False
                'ListBox1.Items.Clear()
                ListBox1.Items.Add(item)
                'AxVecad1.FileOpen(item)
                Button2.Enabled = False
            End If

            If item.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase) = False And item.EndsWith(".dxf", StringComparison.OrdinalIgnoreCase) = False Then
                MsgBox("Non è possibile aprire questo tipo di file", MsgBoxStyle.Exclamation)
            End If
        Next
    End Sub


    Private Sub bkwcommesse_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bkwcommesse.DoWork
        Try
            commesse = My.Computer.FileSystem.GetDirectories("\\Lafer2k3\uff_tec\Commesse\", FileIO.SearchOption.SearchAllSubDirectories)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

    End Sub

    Private Sub bkwcommesse_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bkwcommesse.RunWorkerCompleted
        statocommesse = 1
        CheckBox2.Enabled = True
        Label3.Text = "Ora è possibile eseguire ricerche in commesse"
        VisualizzaDisegniInCartelleClientiToolStripMenuItem.Enabled = True
        rcommesse.Clear()
        salva_impostazioni()
    End Sub

    Private Sub bkwfiles_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bkwfiles.DoWork
        files = My.Computer.FileSystem.GetFiles(cartella, FileIO.SearchOption.SearchTopLevelOnly, stringa & "*.*")
    End Sub

    Private Sub bkwfiles_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bkwfiles.RunWorkerCompleted
        statofiles = 1
    End Sub

    'Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
    '    AxAcroPDF1.printWithDialog()
    'End Sub

    Private Sub bkwprint_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bkwprint.DoWork
        'Me.AxAcroPDF1.printAllFit(True)
    End Sub

    Private Sub bkwprint_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bkwprint.RunWorkerCompleted
        ' MsgBox("Documento inviato alla stampante")
    End Sub

    Private Sub ConfrontaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConfrontaToolStripMenuItem.Click
        Frmconfronta.Show()
        Frmconfronta2.Show()
    End Sub

    Private Sub VisualizzaDisegniInCartelleClientiToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VisualizzaDisegniInCartelleClientiToolStripMenuItem.Click
        FrmDisegniClienti.Show()
    End Sub

    Private Sub Selezionefilepdf() ' impostazioni finestra file selezionato pdf

        'ABILITO ELIMINA
        Button3.Enabled = True
        'ABILITO RINOMINA
        Button4.Enabled = True
        'ABILITO STAMPA
        Button2.Enabled = True
        BtnPrintX2.Enabled = True
        BtnPrintX3.Enabled = True

        'ABILITO STAMPA SPECIALE
        'Button7.Enabled = True
        'VISUALIZZO ACROBAT
        AxAcroPDF1.Show()
        'NASCONDO VECAD
        AxVecad1.Hide()
        Dati_stampa1.Enabled = True
        'SE è INSTALLATO IL PROGRAMMA PDF EDITOR ABILITO IL PULSANTE MODIFICA PDF
        If My.Computer.FileSystem.FileExists(My.Computer.FileSystem.SpecialDirectories.ProgramFiles & "\pdf editor 2\pdfedit.exe") = True Then
            Button5.Enabled = True
        End If


    End Sub

    Private Sub Selezionefiledxf() ' impostazioni finestra file selezionato dxf

        'NASCONDO ACROBAT
        AxAcroPDF1.Hide()
        'VISUALIZZO VECAD
        AxVecad1.Show()
        'APRO IL FILE
        AxVecad1.FileOpen(ListBox1.SelectedItem.ToString)
        'ZOOMO SU TUTTO IL DISEGNO
        AxVecad1.Execute(CAD_CMD_ZOOM_EXT)
        'ABILITO IL PULSANTE ELIMINA
        Button3.Enabled = True
        'ABILITO IL PULSANTE RINOMINA
        Button4.Enabled = True
        'DISABILITO IL PULSANTE STAMPA
        Button2.Enabled = False
        'DISABILITO IL PULSANTE MODIFICA PDF
        Button5.Enabled = False
        'DISABILITO IL PULSANTE STAMPA SPECIALE
        'Button7.Enabled = False
        BtnPrintX2.Enabled = False
        BtnPrintX3.Enabled = False
        Dati_stampa1.Enabled = False

    End Sub

    Private Sub Openpdffile() 'sub per la gestione dei file pdf con watermark
        comp_pdf.dati_stampa.Tempfile = "Temp1"
        'AxAcroPDF1.setZoom(75)

        'SE IL FILE è IN MODIIFCA VISUALIZZO UN MESSAGGIO
        If documento.PDFDOCUMENT.PERCORSO.Contains("Z_PDF_modifica_PROE") Then
            'AGGIUNGO IL WATEMARK
            comp_pdf.dati_stampa.drawstate = "modifica"
            comp_pdf.dati_stampa.file_origine = ListBox1.SelectedItem.ToString
            comp_pdf.dati_stampa.qta_mag = QUANTITA_COMPONENTE
            comp_pdf.dati_stampa.RUN_PDF(comp_pdf.dati_stampa.drawstate, documento.PDFDOCUMENT.PERCORSO_COMPLETO, QUANTITA_COMPONENTE)
            AxAcroPDF1.LoadFile(comp_pdf.dati_stampa.file_modificato)

            Label4.Text = "Attenzione bisogna modificare il pezzo"

            'MsgBox("Attenzione bisogna modificare il pezzo")
            'SE IL FILE è IN PROVVISORI VISUALIZZO UN MESSAGGIO
        ElseIf documento.PDFDOCUMENT.PERCORSO.Contains("Z_PDF_Prov_PROE") Then

            comp_pdf.dati_stampa.drawstate = "provvisori"
            comp_pdf.dati_stampa.file_origine = ListBox1.SelectedItem.ToString
            comp_pdf.dati_stampa.qta_mag = QUANTITA_COMPONENTE
            comp_pdf.dati_stampa.RUN_PDF(comp_pdf.dati_stampa.drawstate, documento.PDFDOCUMENT.PERCORSO_COMPLETO, QUANTITA_COMPONENTE)
            AxAcroPDF1.LoadFile(comp_pdf.dati_stampa.file_modificato)

            Label4.Text = "Attenzione bisogna modificare il Par"
            'MsgBox("Attenzione bisogna modificare il Par")
        Else
            'altrimenti APRO IL PDF
            comp_pdf.dati_stampa.drawstate = ""
            comp_pdf.dati_stampa.file_origine = documento.PDFDOCUMENT.PERCORSO_COMPLETO
            comp_pdf.dati_stampa.qta_mag = QUANTITA_COMPONENTE
            comp_pdf.dati_stampa.RUN_PDF(comp_pdf.dati_stampa.drawstate, documento.PDFDOCUMENT.PERCORSO_COMPLETO, QUANTITA_COMPONENTE)
            AxAcroPDF1.LoadFile(comp_pdf.dati_stampa.file_modificato)
        End If
        Button8_Click1(Me, Nothing)
    End Sub


    Private Sub Button8_Click1(ByVal sender As Object, ByVal e As System.EventArgs)
       
        'ListBox1.SelectedItem = ListBox1.SelectedIndex

        If QUANTITA_COMPONENTE <> "" Or QUANTITA_COMPONENTE <> Nothing Then
            'comp_pdf.dati_stampa.drawstate = ""
            comp_pdf.dati_stampa.file_origine = documento.PDFDOCUMENT.PERCORSO_COMPLETO
            comp_pdf.dati_stampa.qta_mag = QUANTITA_COMPONENTE
            comp_pdf.dati_stampa.RUN_PDF(comp_pdf.dati_stampa.drawstate, documento.PDFDOCUMENT.PERCORSO_COMPLETO, QUANTITA_COMPONENTE)
            comp_pdf.UserControl1.filesorgente_TextChanged(Nothing, Nothing)
        Else
            'comp_pdf.dati_stampa.drawstate = ""
            comp_pdf.dati_stampa.file_origine = documento.PDFDOCUMENT.PERCORSO_COMPLETO
            comp_pdf.dati_stampa.qta_mag = QUANTITA_COMPONENTE
            comp_pdf.dati_stampa.RUN_PDF(comp_pdf.dati_stampa.drawstate, documento.PDFDOCUMENT.PERCORSO_COMPLETO, QUANTITA_COMPONENTE)
            comp_pdf.UserControl1.filesorgente_TextChanged(Nothing, Nothing)
        End If

    End Sub

    Private Sub RicercaDatabaseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RicercaDatabaseToolStripMenuItem.Click
        Timer1.Enabled = False
        frmricercadatabase.Show()
    End Sub

    Private Sub CARTELLAToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CARTELLAToolStripMenuItem.Click
        Dim DESTINAZIONE As String
        FolderBrowserDialog1.ShowDialog()
        DESTINAZIONE = FolderBrowserDialog1.SelectedPath

        '---------------------------GESTIONE OLD
        'Dim NOME As String
        'NOME = My.Computer.FileSystem.GetName(ListBox1.SelectedItem)
        'Try
        '    My.Computer.FileSystem.CopyFile(ListBox1.SelectedItem, DESTINAZIONE & "\" & NOME)
        '    My.Computer.FileSystem.DeleteFile(ListBox1.SelectedItem)
        '    Button1_Click(sender, e)
        'Catch EX As Exception
        '    MsgBox("IL FILE è GIA PRESENTE")
        'End Try

        '--------------------------NUOVA GESTIONE
        If DESTINAZIONE = "" Then
        Else
            For Each ENTRY In ListBox1.SelectedItems
                Dim NOME As String
                NOME = My.Computer.FileSystem.GetName(ENTRY.ToString)
                Try
                    My.Computer.FileSystem.CopyFile(ENTRY.ToString, DESTINAZIONE & "\" & NOME)
                    My.Computer.FileSystem.DeleteFile(ENTRY.ToString)

                Catch ex As Exception
                    MsgBox("IL FILE è GIA PRESENTE")
                End Try
            Next
            Button1_Click(sender, e)
        End If


    End Sub

    Private Sub SuddividiPdf1PaginaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SuddividiPdf1PaginaToolStripMenuItem.Click
        DIVIDI_PDF_FRONTE(ListBox1.SelectedItem)
    End Sub

    Private Sub SuddividiPdf2PagineToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SuddividiPdf2PagineToolStripMenuItem.Click
        DIVIDI_PDF_FRONTE_RETRO(ListBox1.SelectedItem)
    End Sub

    Private Sub CopiaPercorsoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopiaPercorsoToolStripMenuItem.Click
        My.Computer.Clipboard.Clear()
        My.Computer.Clipboard.SetText(ListBox1.SelectedItem)
    End Sub

    Private Sub AggiornamentoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AggiornamentoToolStripMenuItem.Click
        FrmUpdate.ShowDialog()

    End Sub

    Private Sub EliminaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EliminaToolStripMenuItem.Click
        Button3_Click(sender, e)
    End Sub

    Private Sub StampaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StampaToolStripMenuItem.Click
        Button2_Click(sender, e)
    End Sub

    Private Sub RinominaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RinominaToolStripMenuItem.Click
        Button4_Click(sender, e)
    End Sub

    Private Sub VERIFICAPDFToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VERIFICAPDFToolStripMenuItem.Click
        ListBox1.Items.Clear()

        For Each ITEM In My.Computer.FileSystem.GetFiles("\\Lafer2k3\uff_tec\PDF_PROE\", FileIO.SearchOption.SearchAllSubDirectories, stringa & "*.PDF")
            If VERIFICA_VERSIONE(ITEM) <> "" Then
                ListBox1.Items.Add(ITEM)
            End If
        Next
    End Sub

    Private Sub ListBox1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDoubleClick
        If ListBox1.SelectedItem.ToString.EndsWith(".dxf", StringComparison.OrdinalIgnoreCase) = True Then
            Dim PROCESSO = Process.Start(ListBox1.SelectedItem.ToString)
            While Not PROCESSO.HasExited
                Continue While
            End While
        End If
    End Sub

    Private Sub ListBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox2.SelectedIndexChanged
        'ListBox1.SelectedItem = Nothing
        ListBox1.SelectedIndex = ListBox2.SelectedIndex
    End Sub

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        RICERCA_LIST = My.Computer.FileSystem.GetFiles(PERCORSO_RICERCA, FileIO.SearchOption.SearchAllSubDirectories, stringa & "*.*")
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        RICERCA_COMPLETATA = 1
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        If temp1 Is Nothing Then
            If comp_pdf.dati_stampa.file_modificato Is Nothing Then
            Else
                temp1 = My.Computer.FileSystem.GetFileInfo(Application.StartupPath & "\Temp1.pdf")
            End If

        Else
            Dim time As System.IO.FileInfo
            time = My.Computer.FileSystem.GetFileInfo(Application.StartupPath & "\Temp1.pdf")

            If time.LastWriteTime > temp1.LastWriteTime Then
                AxAcroPDF1.LoadFile(Application.StartupPath & "\Temp1.pdf")
                temp1 = time
            End If
        End If
    End Sub

    Private Sub Form1_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated
        Timer1.Enabled = True
        comp_pdf.dati_stampa.Tempfile = "Temp1"
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPrintX2.Click
        For I = 1 To 2
            Dim MyProcess As New Process
            MyProcess.StartInfo.CreateNoWindow = False
            MyProcess.StartInfo.Verb = "print"
            MyProcess.StartInfo.FileName = AxAcroPDF1.src
            MyProcess.Start()
            MyProcess.WaitForExit(4000)
            'MyProcess.CloseMainWindow()
            MyProcess.Close()
        Next
    End Sub

    Private Sub BtnPrintX3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPrintX3.Click
        For I = 1 To 3
            Dim MyProcess As New Process
            MyProcess.StartInfo.CreateNoWindow = False
            MyProcess.StartInfo.Verb = "print"
            MyProcess.StartInfo.FileName = AxAcroPDF1.src
            MyProcess.Start()
            MyProcess.WaitForExit(4000)
            'MyProcess.CloseMainWindow()
            MyProcess.Close()
        Next
    End Sub
End Class
